<?php
class GridViewCheckAll extends CWidget {
    public $totalItemCount;
    public $itemCount;

    public function run() {
        $itemCount = $this->itemCount;
        $totalItemCount = $this->totalItemCount;


        Yii::app()->clientScript->registerScript('definedFunction', "
            function selectAllSubmit(url, confirmMess){
                if($('#is-select-all-item').val() == '1')
                {
                    $('.search-form form').unbind('submit');
                    $('.search-form form').attr('method', 'post');
                    $('.search-form form').attr('action', url);
                    $('.search-form form').submit();
                }
                else
                {
                    if(!$(\"input[name='select-row[]']:checked\").attr('checked')){
                        alert(LANG.DELETE_CHECKBOX_EMPTY);
                        return;
                    }
                    else
                    {
                        if((confirmMess != \"\") && !confirm(confirmMess)) return false;
                        $('#form-admin').attr('action', url);
                        $('#form-admin').submit();
                    }
                }
            }
        ", CClientScript::POS_HEAD);

        Yii::app()->clientScript->registerScript('bindReadyFunction', "
            $('#select-all').live('click',function(){
                $(\"INPUT[type='checkbox']\").attr('checked', $('#select-all').is(':checked'));
                if(this.checked)
                {
                    $('table.items').addClass('selectedAll');
                    if(".$itemCount."<".$totalItemCount.")
                    {
                        var sMessage1 = '".strtr(Yii::t('BackEnd', 'All {itemCount} items on this page are selected.'), array('{itemCount}'=>'<b>'.$itemCount.'</b>'))."';
                        var sMessage2 = '".strtr(Yii::t('BackEnd', 'Select {totalItemCount} items in the found result.'), array('{totalItemCount}'=>'<b>'.$totalItemCount.'</b>'))."';
                        $('table.items tbody').prepend($('<tr id=\"select-all-record\"><td colspan=\"10\" style=\"text-align: center\">'+sMessage1+' <a href=\"javascript:void(0)\" id=\"select-all-link\">'+sMessage2+'</a></td></tr>'));
                        $('#select-all-link').live('click',function(){
                            $('#is-select-all-item').val(1);
                            $('tr#select-all-record td').html(sMessage2+' <a href=\"javascript:void(0)\" id=\"clear-selection\">".Yii::t('BackEnd', 'Clear selection')."</a>');
                            $('#clear-selection').live('click',function(){
                                $('#is-select-all-item').val(0);
                                $('tr#select-all-record td').html(sMessage1+' <a href=\"javascript:void(0)\" id=\"select-all-link\">'+sMessage2+'</a>');
                            });
                        });
                    }
                }
                else
                {
                    $('#is-select-all-item').val(0);
                    $('table.items').removeClass('selectedAll');
                    $('tr#select-all-record').remove();
                }
            });
        ", CClientScript::POS_READY);
    }
}